package com.zhangxiaofanfan.hotcode.third;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <a href="https://leetcode.cn/problems/sort-list/description/?envType=study-plan-v2&envId=top-100-liked">排序链表</a>
 *
 * @author zhangxiaofanfan
 * @date 2024-05-29 08:29:09
 */
public class HotCode033 {
    public static void main(String[] args) {

    }

    public ListNode sortList(ListNode head) {
        if (head == null) {
            return null;
        }
        List<ListNode> list = new ArrayList<>();
        ListNode index = head;
        while (index != null) {
            list.add(index);
            index = index.next;
        }
        list.sort(Comparator.comparingInt(o -> o.val));
        ListNode header = new ListNode();
        index = header;
        while (!list.isEmpty()) {
            index.next = list.removeFirst();
            index = index.next;
        }
        return header.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
